from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
from .models import Teacher,TeacherDetail,Dept,Course


# 添加老师信息
def Add_Teacher(request):
    Teacher.objects.create(
        name = '顾轩2',
        age =22
    )
    return HttpResponse('添加成功')

# 添加老师详情信息。 关系是:一对一
def Add_Teacher_Detail(request):
    teacher = Teacher.objects.create(
        name='顾轩3',
        age=22
    )
    TeacherDetail.objects.create(
        birthday='2001-01-01',
        sex=0,
        phone='19928408054',
        address='湖南省长沙市岳麓区',
        hiredate='2020-10-10',
        teacher = teacher

    )
    return HttpResponse('添加成功')





def update_Teacher(request):
    Teacher.objects.filter(name='顾轩').update(age=23)
    return HttpResponse('修改成功')




def   Update_Teacher_Detail(request):
    id = Teacher.objects.get(name='顾轩3').id
    TeacherDetail.objects.filter(teacher_id=id).update(phone='19928408053')
    return HttpResponse('修改成功')

def Delete_Teacher(request):
    Teacher.objects.delete()
    return HttpResponse('信息删除成功')


# 添加部门和课程信息  关系：一对多
def Add_Dept_Course(request):
    dept_obj = Dept.objects.create(
        name='Python部门'
    )
    Course.objects.create(
        name='python基础教学',
        num=20,
        dept=dept_obj
    )
    return  HttpResponse('添加成功')


def Update_Course(request):
    # Course.objects.filter(id=1).update(dept_id=2)
    dept_id = Dept.objects.get(name='Web部门').id
    Course.objects.filter(id=1).update(dept_id=dept_id)

    return HttpResponse('修改成功')


#课程与老师之间的关系 关系是：多对多
def Add_Course_Teacher(request):
    course  = Course.objects.get(id=1)
    course.teacher.add(2)
    return HttpResponse('添加成功')

def Update_Course_Teacher(request):
    course = Course.objects.get(id=1)
    course.teacher.set('3')

    return HttpResponse('修改成功')

def Delete_Course_Teacher(request):
    course = Course.objects.get(id=1)
    course.teacher.remove(3)

    # 第二种方法 按照模型类对象参数删除
    course = Course.objects.get(id=3)
    teacher = Teacher.objects.get(id=3)
    course.teacher.remove(teacher)

    # 清空id为1课程的所有的关联关系
    course = Course.objects.get(id=3)
    course.teacher.clear()
    return HttpResponse('修改成功')




def Check_Teacher(request):
    #正向查询
    teacher_obj= TeacherDetail.objects.get(phone='19928408053')
    print(teacher_obj.teacher.name)

    #反向查询
    teacher_obj2  = Teacher.objects.get(name='顾轩3')
    print(teacher_obj2)
    print(teacher_obj2.teacherdetail.address)

    return HttpResponse('查询成功')



def  Check_Course_Dept(request):
     # 正向查询
     course_obj = Course.objects.get(name='python基础教学')
     print(course_obj.dept.name)

     dept_obj = Dept.objects.get(name='Web部门')
     print(dept_obj.course_set.all().values('name'))

     return HttpResponse('查询成功')



def Check_Course_Teacher(request):
    # 正向查询
    course = Course.objects.get(name='python基础教学')
    print(course.teacher.all().values('name'))

    # 反向查询
    teacher = Teacher.objects.get(name='顾轩')
    print(teacher.course_set.all().values('name'))

    return HttpResponse('查询成功')




def   Check_Teacher1(request):
    # 正向查询
    teacher_obj = TeacherDetail.objects.filter(phone='19928408053').values('teacher__name')
    print(teacher_obj)


    # 反向查询
    teacher_obj2 = Teacher.objects.filter(teacherdetail__phone='19928408053').values('name')
    print(teacher_obj2)



    return HttpResponse('查询成功')